/**
 * CLEAN TASKs
 * @Author: jiangcheng198745@hotmial.com
 * @Date: 2016/07/09
 */
'use strict';

import gulp from 'gulp';
import filter from 'gulp-filter';
import $if from 'gulp-if';
import print from 'gulp-print';
import {argv as args} from 'yargs';
import {reload} from 'browser-sync';

import messageLogger  from '../utils/message.gulp';
import logWatch from '../utils/logWatch.gulp';
import config from '../gulp.config';
let changedFiles, testFiles;

gulp.task('watch', [
    'watch:scss',
    'watch:js',
    'watch:jsx',
    'watch:json',
    'watch:vendorjs',
    'watch:templates'
]);

gulp.task('watch:js', () => {
    if (args.dist) return ;
    gulp.watch([
        config.appFiles.js
    ], [
        'browserify'
    ])
    .on('change', karmaModifiedFiles);
   
});

gulp.task('watch:vendorjs', () => {
    if(args.dist) return;
    gulp.watch([
        config.vendorFiles.js
    ], [
        'copy:build-vendor-js',
        'index:build'
    ]);
});
gulp.task('watch:json', () => {
    gulp.watch([
        config.appFiles.json
    ], [
        'browserify'
    ]);
});

gulp.task('karma-watch', () =>{

});

gulp.task('copy:changed-files', () => {
    messageLogger('Copying changed files...', changedFiles);
    let fileFilter = filter(['*', '**/*'], {
        restore: true
    });
    console.log(changedFiles);
    return gulp.src(changedFiles, {
        base: config.app
    })
    .pipe(fileFilter)
    .pipe(gulp.dest(config.build))
    .pipe(fileFilter.restore)
    .pipe(reload({
        stream: true
    }))

});

function karmaModifiedFiles(event) {
    let files = logWatch(event);
    changedFiles = files.changedFiles;
    testFiles = files.testFiles
}
